package com.keyancompany.zhongbaopingtai.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.TeamUsersAdapter;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.OtherTeam;
import com.keyancompany.zhongbaopingtai.model.TeamUser;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.EmptyView;

import butterknife.Bind;
import butterknife.BindString;

/**
 * 转让组员列表
 * Created by Gia on 2016/9/6.
 */
public class TeamUsersActivity extends BaseActivity {
    @Bind(R.id.empty_view)
    EmptyView emptyView;
    @Bind(R.id.list)
    RecyclerView mRecyclerView;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @BindString(R.string.member_list_text)
    String title;

    String teamCode;
    private LinearLayoutManager mLayoutManager;
    private TeamUsersAdapter mAdapter;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_teamusers;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {
        teamCode = SPUtils.getComment(mContext).getString(Constants.TEAM_CODE, null);
        //创建默认的线性LayoutManager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRecyclerView.setHasFixedSize(true);


        initData();
    }

    private void initData() {
        Log.e("TT", "团员列表");
        if (isNetvaiable()) {
            //团队转让其他成员列表
            teamCode = SPUtils.getComment(mContext).getString(Constants.TEAM_CODE, null);

            TeamAPI.otherTeam(teamCode, TeamUsersActivity.this, getLoading(R.string.watting_hint));
        }
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        switch (type) {
            case TeamAPI.OTHER_TEAM_MARK:
                if (validateJson(json) != null) {
                    json = "{\"result\":\"sucess\",\"token\":\"111111\",\"data\":{\"teamcode\":\"add612bb-10b8-4151-90fc-c986d2853fa9\",\"userId\":\"402881f855e7facd0155e7feb9350001\",\"teamusers\":[{\"phone\":null,\"projectCode\":\"402881f855ec83db0155ec8e4b330001\",\"teamCode\":\"add612bb-10b8-4151-90fc-c986d2853fa9\",\"userCode\":\"402881ee55e755ec0155e7ea35de0001\",\"remark\":null,\"state\":null,\"isprovisional\":\"1\",\"id\":57,\"joinrole\":null,\"iscaptain\":\"0\",\"username\":\"冰魄\",\"flag\":null,\"joinTime\":\"2016-07-15 11:20:16 \",\"email\":null,\"role\":\"创始人\"},{\"phone\":null,\"projectCode\":\"402881f855ec83db0155ec8e4b330001\",\"teamCode\":\"add612bb-10b8-4151-90fc-c986d2853fa9\",\"userCode\":\"402881ee55e755ec0155e7ff906c0003\",\"remark\":null,\"state\":null,\"isprovisional\":\"1\",\"id\":58,\"joinrole\":null,\"iscaptain\":\"1\",\"username\":\"yinquan1\",\"flag\":null,\"joinTime\":\"2016-07-15 11:20:16 \",\"email\":null,\"role\":null}]},\"msg\":\"查询成功\"}";
                    Log.e("TT", "组员！json" + json);

                    BaseModel base = JSONObject.parseObject(json, BaseModel.class);
                    OtherTeam other = JSONObject.parseObject(base.data, OtherTeam.class);
                    TeamUser teamUsers = JSONObject.parseObject(base.data, TeamUser.class);

                    if (base.result.equals(Constants.SUCCESS)) {
                        Log.e("TT", "组员！" + teamUsers);
                        if (other.getTeamusers().size() > 0) {
                            emptyView.setVisibility(View.GONE);
                            //创建并设置Adapter
                            mAdapter = new TeamUsersAdapter(other.getTeamusers());
                            mRecyclerView.setAdapter(mAdapter);
//                            mAdapter.setOnItemClickListener(new TeamUsersAdapter.OnRecyclerViewItemClickListener(){
//                                @Override
//                                public void onItemClick(View view , String teamId){ //转让团队
//                                    TeamAPI.setLeader(teamCode, teamId, TeamUsersActivity.this, getLoading(R.string.watting_hint));
//                                }
//                            });

                        } else {
                            T.showShort(mContext, "当前暂无组员信息！");
                            emptyView.setVisibility(View.VISIBLE);
                        }
                    } else {
                        T.showShort(mContext, base.msg);
                    }
                }

                break;
            case TeamAPI.SET_LEADER_MARK:
                if (validateJson(json) != null) {
                    BaseModel base = JSONObject.parseObject(json, BaseModel.class);
                    if (base.result.equals(Constants.SUCCESS)) {
                    } else {
                        T.showShort(mContext, base.msg);
                    }
                }
                break;
        }
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        T.showShort(mContext, error);
    }


}
